/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.util;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;

import com.wx.paintback.PaintHelper;

import androidx.annotation.ColorInt;
import tw.com.hitevision.whiteboard.android.Device;

public class PaintManager {
    private static final String TAG = PaintManager.class.getSimpleName();

    private PaintHelper paintHelper;

    private boolean isDrawing;

    public PaintManager(Context context) {
        paintHelper = new PaintHelper(context);
    }

    public void close() {
        if (isDrawing) {
            stop();
        }
        paintHelper.close();
    }

    public void start(MotionEvent event) {
        log("start");
        paintHelper.setEvent(event);
        paintHelper.sendOrder(PaintHelper.ORDER_TYPE.ORDER_SET_DRAW, 1);
        isDrawing = true;
    }


    public void stop() {
        log("stop");
        paintHelper.sendOrder(PaintHelper.ORDER_TYPE.ORDER_SET_DRAW, 0);
        isDrawing = false;
    }

    public boolean isDrawing() {
        return isDrawing;
    }

    public void setWidth(Device device, float width) {
//        log("setWidth: device=" + device + ", width=" + width);
        final PaintHelper.ORDER_TYPE orderType = device == Device.FINGER
                ? PaintHelper.ORDER_TYPE.ORDER_WIDTH_FINGER
                : PaintHelper.ORDER_TYPE.ORDER_WIDTH_PEN;
        paintHelper.sendOrder(orderType, (int) width);
    }

    public void setColor(Device device, @ColorInt int color) {
//        log("setColor: device=" + device + ", color=" + color);
        final PaintHelper.ORDER_TYPE orderType = device == Device.FINGER
                ? PaintHelper.ORDER_TYPE.ORDER_COLOR_FINGER
                : PaintHelper.ORDER_TYPE.ORDER_COLOR_PEN;
        paintHelper.sendOrder(orderType, color);
    }

    public void onTouch(MotionEvent event) {
        // do nothing
    }

    private static void log(String msg) {
        Log.d(TAG, msg);
    }
}
